﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bluyel.Core;

namespace Bluyel_Samples_VS10
{
    /// <summary>
    /// Code samples to see how to create:
    /// Var or Const of DVal of phyLength (Physical Quantity Length).
    /// </summary>
    public class Sample_VarConst_PhyLength
    {
        //---------------------------------------------------------------------
        /// <summary>
        /// Create the type Physical Quantity Length, configure
        /// and define some data values.
        /// </summary>
        public void CreateVars()
        {
            Console.WriteLine("\n----Create physical qty length variables and constants:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create the var a: PhyLength a=12m
            IDPhyLength varA = core.DataBuilder.CreateVarPhyLength("a");
            core.DataBuilder.SetVal(varA, 12, TypePhyLengthPrefix.Metre);

            // create the var b: PhyLength b=27cm
            IDPhyLength varB = core.DataBuilder.CreateVarPhyLength("a", 27, TypePhyLengthPrefix.Centimetre);

            // create a constant phy length value
            IDPhyLength constC = core.DataBuilder.CreateConstPhyLength("c", 3.89, TypePhyLengthPrefix.Kilometre);

            // display values fo the  vars
            Console.WriteLine("Create: " + varA.VarOrConst + " " + varA.Type.Name + " " + varA.Name + "=" + varA.DVal.Value + " " + varA.Prefix);
            Console.WriteLine("Create: " + varB.VarOrConst + " " + varB.Type.Name + " " + varB.Name + "=" + varB.DVal.Value + " " + varB.Prefix);
            Console.WriteLine("Create: " + constC.VarOrConst + " " + constC.Type.Name + " " + constC.Name + "=" + constC.DVal.Value + " " + constC.Prefix);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// create a const, try to change the value.
        /// </summary>
        public void CreateConstTryChangeVal()
        {
            Console.WriteLine("\n----Create a const, try to change a value:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // Create a const: int a=10
            // create the var a: PhyLength a=12m
            IDPhyLength constA = core.DataBuilder.CreateConstPhyLength("a", 23, TypePhyLengthPrefix.Decimetre);
            Console.WriteLine("Create :" + constA.VarOrConst + " " + constA.Type + " " + constA.Name + "=" + constA.DVal.Value + " " + constA.Prefix);

            // now set a value to the int: should failed!
            bool res = core.DataBuilder.SetVal(constA, 27);
            Console.WriteLine("Try to modify the constant: " + res);

            //// display values fo the  vars
            Console.WriteLine("So (no change):" + constA.VarOrConst + " " + constA.Type + " " + constA.Name + "=" + constA.DVal.Value + " " + constA.Prefix);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// </summary>
        public void Compare2Vars()
        {
            Console.WriteLine("\n----Physical qty length, compare variables:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create the var a: PhyLength a=12m
            IDPhyLength varA = core.DataBuilder.CreateVarPhyLength("a");
            core.DataBuilder.SetVal(varA, 12, TypePhyLengthPrefix.Metre);

            // create the var b: PhyLength b=27cm
            IDPhyLength varB = core.DataBuilder.CreateVarPhyLength("a", 27, TypePhyLengthPrefix.Centimetre);

            // compare, Is a (12m) > b (27cm) ? = true
            bool res = core.PhQConverter.IsGreaterThan(varA.DVal, varB.DVal);
            Console.WriteLine("Is var a (12m) > var b (27cm) ? " + res);

            // compare, Is a (12m) < b (27cm) ? = false
            res = core.PhQConverter.IsLessThan(varA.DVal, varB.DVal);
            Console.WriteLine("Is var a (12m) < var b (27cm) ? " + res);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// </summary>
        public void ConvertValue()
        {
            Console.WriteLine("\n----Physical qty length, Convert a variable value:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create the var a: PhyLength a=12m
            IDPhyLength varA = core.DataBuilder.CreateVarPhyLength("a");
            core.DataBuilder.SetVal(varA, 12, TypePhyLengthPrefix.Metre);

            // convert the dv in cm: 12m= 1200cm 
            DValPhyLength dvLength12mToCm = core.PhQConverter.ConvertTo(varA.DVal, TypePhyLengthPrefix.Centimetre);
            Console.WriteLine("Convert var a= 12m to cm: " + dvLength12mToCm.Value + " " + dvLength12mToCm.Prefix);
        }
    }
}
